﻿using System;
using System.Web.Mvc;
using System.Web.Routing;
using Core.IoC;
using Web.Controllers;

namespace Web
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute(
                "Default",                                              // Route name
                "{controller}/{action}/{id}",                           // URL with parameters
                new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
            );

        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            SetUpControllerFactory();
        }

        private void SetUpControllerFactory()
        {
            // Get all of the types in this assembly.
            Type[] types = typeof(HomeController).Assembly.GetTypes();
            foreach (Type type in types)
            {
                // If the type implements IController, register the type with the IOC container.
                if (typeof(IController).IsAssignableFrom(type) && !type.IsAbstract)
                {
                    IoC.AddType(type.Name, type, IoC.LifeStyle.Transient);
                }
            }
            // Tell the MVC framework to use the WindsorControllerFactory to create controllers.
            ControllerBuilder.Current.SetControllerFactory(typeof(WindsorControllerFactory));
        }
    }
}